import request from '@/utils/request'
import qs from 'qs'

export function initData(url, params, method = 'get') {
    params = changePageAndSize(url, params)
    if (method === 'get') {
        return request({
            url: url + '?' + qs.stringify(params, { indices: false }),
            method: 'get'
        })
    } else {
        return request({
            url: url,
            method,
            data: params
        })
    }
}

function changePageAndSize(url, params) {
    const changeList = ['admin/dic/list', 'admin/dic/item/list', 'admin/treatment/list', 'admin/programme/list', 'admin/reservation/patient/list', 'admin/reservation/treatment/list', 'admin/reservation/treatment/exe/list', 'admin/reservation/treatment/reservation/list']
    if (changeList.includes(url)) {
        return {
            ...params,
            page: params.pageIndex,
            size: params.pageSize,
            pageIndex: undefined,
            pageSize: undefined
        }
    } else {
        return params
    }
}

export function download(url, params) {
    return request({
        url: url + '?' + qs.stringify(params, { indices: false }),
        method: 'get',
        responseType: 'blob'
    })
}
